using System.Collections;
using System.Collections.Generic;
using FrameWork;
using FrameWork.Inventory;
using FrameWork.TileMapManager;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SaveManager : SingletonMonoBehaviour<SaveManager>
{
    [SerializeField] private NameDataSO nameDataSO;
    
    [SerializeField] private SaveData saveData;
    [SerializeField] private LoadData loadData;

    /// <summary>
    /// 创建存档，输入存档名称
    /// 初始化所有Model层数据
    /// </summary>
    /// <param name="saveName"></param>
    public void CreatGameData(string saveName)
    {
        nameDataSO.GameDataName = saveName;
        
        // 创建存档
        SaveDataInfo saveDataInfo = saveData.CreatSaveDataInfo();
        
        // 保存本地记录
        SaveDataSystem.SaveByJson(nameDataSO.GameDataName, saveDataInfo);
        
        // 读取保存的数据
        LoadGameData();
    }

    /// <summary>
    /// 保存玩家数据
    /// 读取所有Model层数据
    /// 保存
    /// </summary>
    public void SaveGameData()
    {
        // 创建存档
        SaveDataInfo saveDataInfo = saveData.GetSaveDataInfo();
        
        // 保存本地记录
        SaveDataSystem.SaveByJson(nameDataSO.GameDataName, saveDataInfo);
    }

    public void LoadGameData(string saveName)
    {
        nameDataSO.GameDataName = saveName;
        LoadGameData();
    }

    public void LoadGameData()
    {
        SaveDataInfo data = SaveDataSystem.LoadFromJson<SaveDataInfo>(nameDataSO.GameDataName);
        loadData.SetData(data);
        StartCoroutine(Initialize());
    }

    private IEnumerator Initialize()
    {
        // 等待 0.5 秒 加载关卡
        yield return new WaitForSecondsRealtime(1f);
        
        SceneManager.LoadScene(1);
    }
}
